package com.tjec.project.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.tjec.common.utils.StringUtils;
import com.tjec.project.businessWorkbench.domain.BmsBusiness;
import com.tjec.project.businessWorkbench.mapper.BmsBusinessMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tjec.project.system.mapper.BmsAttachmentProjectBusinessMapper;
import com.tjec.project.system.domain.BmsAttachmentProjectBusiness;
import com.tjec.project.system.service.IBmsAttachmentProjectBusinessService;
import org.springframework.util.CollectionUtils;

/**
 * 附件项目业务关联Service业务层处理
 *
 * @author tjec
 * @date 2020-07-19
 */
@Service
public class BmsAttachmentProjectBusinessServiceImpl implements IBmsAttachmentProjectBusinessService
{
    @Autowired
    private BmsAttachmentProjectBusinessMapper bmsAttachmentProjectBusinessMapper;

    @Autowired
    private BmsBusinessMapper bmsBusinessMapper;

    /**
     * 查询附件项目业务关联
     *
     * @param attachmentId 附件项目业务关联ID
     * @return 附件项目业务关联
     */
    @Override
    public List<BmsAttachmentProjectBusiness> selectBmsAttachmentProjectBusinessByGuid(String attachmentId)
    {
        return bmsAttachmentProjectBusinessMapper.selectBmsAttachmentProjectBusinessByGuid(attachmentId);
    }

    /**
     * 查询附件项目业务关联列表
     *
     * @param bmsAttachmentProjectBusiness 附件项目业务关联
     * @return 附件项目业务关联
     */
    @Override
    public List<BmsAttachmentProjectBusiness> selectBmsAttachmentProjectBusinessList(BmsAttachmentProjectBusiness bmsAttachmentProjectBusiness)
    {
        return bmsAttachmentProjectBusinessMapper.selectBmsAttachmentProjectBusinessList(bmsAttachmentProjectBusiness);
    }

    /**
     * 新增附件项目业务关联
     *
     * @param bmsAttachmentProjectBusiness 附件项目业务关联
     * @return 结果
     */
    @Override
    public int insertBmsAttachmentProjectBusiness(BmsAttachmentProjectBusiness bmsAttachmentProjectBusiness)
    {
        if(checkIsRepeat(bmsAttachmentProjectBusiness)){
            return 0;
        }
        return bmsAttachmentProjectBusinessMapper.insertBmsAttachmentProjectBusiness(bmsAttachmentProjectBusiness);
    }

    @Override
    public int insertBmsAttachmentProjectBusinessByBusinessId(List<String> attachmentIds, String businessId) {
        if(StringUtils.isNotBlank(businessId)){
            BmsBusiness bmsBusiness  = bmsBusinessMapper.selectBmsBusinessByGuid(businessId);
            BmsAttachmentProjectBusiness bmsAttachmentProjectBusiness = new BmsAttachmentProjectBusiness();
            if(bmsBusiness!=null){
                bmsAttachmentProjectBusiness.setBusinessId(businessId);
                bmsAttachmentProjectBusiness.setProjectId(bmsBusiness.getProjectId());
                this.insertBmsAttachmentProjectBusinessByProjectIdAndBusinessId(attachmentIds,bmsBusiness.getProjectId(), businessId);
            }
        }
        return 0;
    }

    @Override
    public int insertBmsAttachmentProjectBusinessByProjectIdAndBusinessId(List<String> attachmentIds, String projectId, String businessId) {
        int result = 0;
        BmsAttachmentProjectBusiness bmsAttachmentProjectBusiness = new BmsAttachmentProjectBusiness();
        if(StringUtils.isNotBlank(businessId)&&StringUtils.isNotBlank(projectId)){
            bmsAttachmentProjectBusiness.setBusinessId(businessId);
            bmsAttachmentProjectBusiness.setProjectId(projectId);
            if(!CollectionUtils.isEmpty(attachmentIds)){
                for (int i = 0; i < attachmentIds.size(); i++) {
                    bmsAttachmentProjectBusiness.setAttachmentId(attachmentIds.get(i));
                    if(!checkIsRepeat(bmsAttachmentProjectBusiness)){
                        bmsAttachmentProjectBusinessMapper.insertBmsAttachmentProjectBusiness(bmsAttachmentProjectBusiness);
                        result++;
                    }
                }
            }
        }
        return result;
    }

    private boolean checkIsRepeat(BmsAttachmentProjectBusiness bmsAttachmentProjectBusiness){
        List<BmsAttachmentProjectBusiness> list = selectBmsAttachmentProjectBusinessList(bmsAttachmentProjectBusiness);
        if(CollectionUtils.isEmpty(list)){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 修改附件项目业务关联
     *
     * @param bmsAttachmentProjectBusiness 附件项目业务关联
     * @return 结果
     */
    @Override
    public int updateBmsAttachmentProjectBusiness(BmsAttachmentProjectBusiness bmsAttachmentProjectBusiness)
    {
        return bmsAttachmentProjectBusinessMapper.updateBmsAttachmentProjectBusiness(bmsAttachmentProjectBusiness);
    }

    /**
     * 批量删除附件项目业务关联
     *
     * @param attachmentIds 需要删除的附件项目业务关联ID
     * @return 结果
     */
    @Override
    public int deleteBmsAttachmentProjectBusinessByGuids(String[] attachmentIds)
    {
        return bmsAttachmentProjectBusinessMapper.deleteBmsAttachmentProjectBusinessByGuids(attachmentIds);
    }

    /**
     * 删除附件项目业务关联信息
     *
     * @param attachmentId 附件项目业务关联ID
     * @return 结果
     */
    @Override
    public int deleteBmsAttachmentProjectBusinessByGuid(String attachmentId)
    {
        return bmsAttachmentProjectBusinessMapper.deleteBmsAttachmentProjectBusinessByGuid(attachmentId);
    }

    @Override
    public List<String> selectBmsAttachmentProjectBusinessListByProjectId(List<String> projectIdList) {
    	List<String> resList = new ArrayList<String>();
    	if(projectIdList!=null && projectIdList.size()>0) {
    		return bmsAttachmentProjectBusinessMapper.selectBmsAttachmentProjectBusinessListByProjectId(projectIdList);
    	}else {
    		return resList;
    	}
        
    }
}
